﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

using Jarvis.Analytics;
using Jarvis.Learners;
using Jarvis.Rules;
using Jarvis.Sensors;

namespace Jarvis.Windows
{
    /// <summary>
    /// Interaction logic for RulesWindow.xaml
    /// </summary>
    public partial class RulesWindow : Window
    {
        private Boolean RulesListInited = false;
        public LearnerManager learnerManager = null;
        public ObservableCollection<String> sensors { get; set; }

        protected override void OnActivated(EventArgs e)
        {
            if (learnerManager != null && !RulesListInited)
            {
                rulesList.ItemsSource = learnerManager.Rules;
                RulesListInited = true;

                List<String> contexts = new List<string>();
                contexts.Add("Activity");
                contexts.Add("Location");
                contexts.Add("Social Context");

                ContextCombo.ItemsSource = contexts;
            }

            SensorNotifier.Instance.SensorsUpdate += new SensorNotifier.SensorsUpdatedHandler(this.SensorsUpdated);
        }

        public void SensorsUpdated(object obj, EventArgs eventArgs)
        {
            Dictionary<String, object> state = SensorNotifier.Instance.getState();

            foreach (String key in state.Keys)
            {
                if (!sensors.Contains(key))
                     sensors.Add(key);
            }
        }

        public RulesWindow()
        {
            sensors = new ObservableCollection<String>();

            InitializeComponent();
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                (DispatcherOperationCallback)delegate(object o)
                {
                    Hide();
                    return null;
                }, null);

            AnalyticsManager.LogEvent("Window Closing", "Rules");

            e.Cancel = true;
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            Rule r = new Rule();
            r.Name = "New Rule";
            r.And = true;
            r.ContextElement = "Activity";
            r.Outcome = "Away";

            learnerManager.Rules.Add(r);
        }

        private void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            learnerManager.Rules.Remove((Rule) rulesList.SelectedItem);
            rulesList.ItemsSource = learnerManager.Rules;
        }

        private void AddConditionButton_Click(object sender, RoutedEventArgs e)
        {
            Rule r = (Rule) rulesList.SelectedItem;

            RuleComponent rc = new RuleComponent();
            rc.Operation = RuleComponent.Less;
            rc.Value = "0.01";
            rc.Field = "User Activity Level";

            r.Components.Add(rc);
        }

        private void RemoveConditionButton_Click(object sender, RoutedEventArgs e)
        {
            Rule r = (Rule)rulesList.SelectedItem;

            ObservableCollection<RuleComponent> components = r.Components;

            if (components != null)
                components.Remove((ConditionsComboBox.SelectedItem as RuleComponent));
        }
    }
}
